import { defineSource } from '../source';
import { ProcessedItem } from '../types'
import { myFetch } from "../utils/fetch"
import logger from '../../framework/config/logger';
import { md5, myCrypto } from '../utils/crypto';

interface Item {
    id: string
    title?: string
    brief: string
    shareurl: string
    // need *1000
    ctime: number
    // 1
    is_ad: number,
    readNum: string
}

interface Hot {
    data: Item[]
}

const params = {
    appName: "CailianpressWeb",
    os: "web",
    sv: "7.7.5",
}

async function getSearchParams(moreParams?: any) {
    const searchParams = new URLSearchParams({ ...params, ...moreParams })
    searchParams.sort()
    searchParams.append("sign", await md5(await myCrypto(searchParams.toString(), "SHA-1")))
    return searchParams
}


export default defineSource({
    cls: async (): Promise<ProcessedItem[]> => {
        const apiUrl = `https://www.cls.cn/v2/article/hot/list`
        const res: Hot = await myFetch(apiUrl, {
            query: Object.fromEntries(await getSearchParams()),
        })
        return res.data.map((k) => {
            return {
                id: k.id,
                title: k.title || k.brief,
                url: `https://www.cls.cn/detail/${k.id}`,
                extra: {
                    hot: k.readNum,
                    content: k.brief,
                },
            }
        })
    }
});